package gui.ex.paint;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class DoubleBuffering extends JPanel {

	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		g.setColor(Color.RED);
		g.drawLine(0, 0, 200, 200);
	}

	public static void main(String[] args) {
		JFrame frame = new JFrame();
		JPanel panel = new JPanel() {
			protected void paintComponent(Graphics g) {
				BufferedImage bImg = new BufferedImage(200, 200,
						BufferedImage.TYPE_INT_RGB);
				for (int i = 0; i < 200; i++) {
					for (int j = 0; j < 200; j++) {
						if (i % 2 == 0 && j % 2 == 0)
							bImg.setRGB(i, j, 0xFFFFFF);
					}
				}
				// Graphics g = bImg.createGraphics();
				g.setColor(Color.RED);
				g.drawImage(bImg, 0, 0, null);
				g.drawLine(0, 0, 200, 200);
			};
		};

		panel.setPreferredSize(new Dimension(200, 200));
		frame.add(panel);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setLocationRelativeTo(null);
		frame.pack();
		frame.setVisible(true);
	}
}
